package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.entity.ApplicationForm;
import com.evil.common.application.util.ApplicationRedisKeyUtil;
import com.evil.common.core.constant.RedisConstant;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.core.util.CacheMapperUtil;
import com.evil.common.core.util.StreamUtil;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 应用表单表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface ApplicationFormMapper extends BaseMapper<ApplicationForm> {

    Optional<ApplicationForm> isExistById(Long id);

    default Optional<ApplicationForm> isExistById_Cache(Long id) {
        return CacheMapperUtil.cacheById(
                id,
                ApplicationForm.class,
                this::findById,
                ApplicationRedisKeyUtil::requestUuidFormKey,
                RedisConstant.REQUEST_UUID_EXPIRE_IN_SECOND
        );
    }

    default ApplicationForm findById(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_NOT_EXIST));
    }

    default ApplicationForm findById_Cache(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_NOT_EXIST));
    }

    Optional<ApplicationForm> isExistByAppIdAndVersion(@Param("applicationId") Long applicationId, @Param("version") Integer version);

    default ApplicationForm findByAppIdAndVersion(Long applicationId, Integer version) {
        return this.isExistByAppIdAndVersion(applicationId, version).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_NOT_EXIST));
    }

    List<ApplicationForm> findByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    default List<ApplicationForm> findByIds_Cache(Collection<Long> ids, boolean filterDeleted) {
        return CacheMapperUtil.cacheByIds(
                ids,
                filterDeleted,
                ApplicationForm.class,
                this::findByIds,
                ApplicationForm::getFormId,
                ApplicationForm::getIsDeleted,
                null,
                ApplicationRedisKeyUtil::requestUuidFormKeyMap,
                RedisConstant.REQUEST_UUID_EXPIRE_IN_SECOND
        );
    }

    List<ApplicationForm> findByAppIdsAndVersion(@Param("applicationIds") Collection<Long> applicationIds, @Param("version") Integer version);

    default Map<Long, ApplicationForm> findMapByIds(Collection<Long> ids, boolean filterDeleted) {
        return StreamUtil.toMapK(this.findByIds(ids, filterDeleted), ApplicationForm::getFormId);
    }

    default Map<Long, ApplicationForm> findMapByAppIdsAndVersion(Collection<Long> ids, Integer version) {
        return StreamUtil.toMapK(this.findByAppIdsAndVersion(ids, version), ApplicationForm::getApplicationId);
    }
}